package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.AutobrandDao;
import io.renren.dto.AutobrandDTO;
import io.renren.entity.AutobrandEntity;
import io.renren.service.AutobrandService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-21
 */
@Service
public class AutobrandServiceImpl extends CrudServiceImpl<AutobrandDao, AutobrandEntity, AutobrandDTO> implements AutobrandService {

    @Override
    public QueryWrapper<AutobrandEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AutobrandEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<Map<String, Object>> getHotCarBrandList() {
        QueryWrapper<AutobrandEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoBrandOID,Code,Name,AutoBrandPic");
        wrapper.eq("IsHot", 1);
        wrapper.apply("StopDate is  null or StopDate = '1900-01-01'");
        wrapper.orderByAsc("Name");
        List<AutobrandEntity> list = this.baseDao.selectList(wrapper);
        if (!list.isEmpty()) {
            return list.stream().map(item -> {
                Map<String, Object> map = new HashMap<>();
                map.put("AutoBrandOID", item.getAutobrandoid());
                map.put("Code", item.getCode());
                map.put("Name", item.getName());
                map.put("AutoBrandPic", item.getAutobrandpic());
                return map;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<Map<String, Object>> getCarBrandList() {
        QueryWrapper<AutobrandEntity> wrapper = new QueryWrapper<>();
        wrapper.select("AutoBrandOID,Code,Name,AutoBrandPic");
        wrapper.apply("StopDate is  null or StopDate = '1900-01-01'");
        wrapper.orderByAsc("Name");
        List<AutobrandEntity> list = this.baseDao.selectList(wrapper);
        if (!list.isEmpty()) {
            return list.stream().map(item -> {
                Map<String, Object> map = new HashMap<>();
                map.put("AutoBrandOID", item.getAutobrandoid());
                map.put("Code", item.getCode());
                map.put("Name", item.getName());
                map.put("AutoBrandPic", item.getAutobrandpic());
                return map;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public AutobrandEntity findOneByAutoBrandOID(String autobrandFk) {
        QueryWrapper<AutobrandEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("AutoBrandOID", autobrandFk);
        List<AutobrandEntity> list = this.baseDao.selectList(wrapper);
        return list.size() > 0 ? list.get(0) : null;
    }
}